package com.atguigu.gulimall.ware.listener;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.to.mq.OrderTo;
import com.atguigu.common.to.mq.StockLockedTo;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.entity.WareOrderTaskDetailEntity;
import com.atguigu.gulimall.ware.entity.WareOrderTaskEntity;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.gulimall.ware.vo.OrderVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author baiWT
 * @Description
 * @Date 2022/4/12 17:02
 **/
@Component
@RabbitListener(queues = "stock.release.stock.queue")
public class StockReleaseListener {

    @Resource
    private WareSkuService wareSkuService;

    @RabbitHandler
    public void handleStockLockedRelease(StockLockedTo locked, Message message, Channel channel) throws IOException {
        System.out.println("收到库存解锁消息");
        try {
            wareSkuService.StockLockedRelease(locked,message,channel);
            System.out.println("收到库存解锁消息,已解锁");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            System.out.println("消息拒绝以后重新放入队列，让别人继续消费解锁");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }

    @RabbitHandler
    public void handleStockLockedRelease(OrderTo to, Message message, Channel channel) throws IOException {
        System.out.println("订单过期消息");
        try {
            wareSkuService.StockLockedRelease(to);
            System.out.println("收到库存解锁消息,已解锁");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            System.out.println("消息拒绝以后重新放入队列，让别人继续消费解锁");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
